package com.shouxin.attendance.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shouxin.attendance.R;
import com.shouxin.attendance.constants.PrefKey;
import com.shouxin.attendance.utils.SharedPref;
import com.shouxin.attendance.utils.UIUtils;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class ClearAlarmActivity extends BaseActivity implements View.OnClickListener{

	private static final Logger logger = Logger.getLogger(ClearAlarmActivity.class);

    private Button mBtnAdd;
    private Button mBtnDelete;
    private EditText mEditCard;
    private RecyclerView mCardRecyclerView;

    private List<String> mCardNumbers = new ArrayList<>();
    private CardNumberAdapter mAdapter;
    private List<String> mSelectedCards = new ArrayList<>();

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_clear_alarm);
    }

    @Override
    protected void initUIViews() {
        mBtnAdd = (Button) findViewById(R.id.btn_add);
        mBtnDelete = (Button) findViewById(R.id.btn_delete);
        mEditCard = (EditText) findViewById(R.id.et_card_number);
        mCardRecyclerView = (RecyclerView) findViewById(R.id.card_recyclerView);
    }

    @Override
    protected void initData() {
        Set<String> cardNumbers = SharedPref.getSetString(PrefKey.SET_CARD_NUMBER);
        if (cardNumbers == null) {
            cardNumbers = new TreeSet<>();
            SharedPref.putSetString(PrefKey.SET_CARD_NUMBER, cardNumbers);
        } else {
            mCardNumbers.addAll(cardNumbers);
        }
    }

    @Override
    protected void bindEvents() {
        mCardRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mCardRecyclerView.setHasFixedSize(true);
        mCardRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mCardRecyclerView.setNestedScrollingEnabled(false);
        mAdapter = new CardNumberAdapter();
        mCardRecyclerView.setAdapter(mAdapter);

        mBtnAdd.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Set<String> cardNumbers = SharedPref.getSetString(PrefKey.SET_CARD_NUMBER);
        if (v == mBtnAdd) {
            String cardNumber = mEditCard.getText().toString().trim();

            if (cardNumbers.contains(cardNumber)) {
                UIUtils.toast("提示：卡片已存在！");
            } else {
                cardNumbers.add(cardNumber);
                SharedPref.putSetString(PrefKey.SET_CARD_NUMBER, cardNumbers);
                mCardNumbers.add(cardNumber);
                mAdapter.notifyDataSetChanged();
            }
            mEditCard.setText("");
        } else if (v == mBtnDelete) {
            cardNumbers.removeAll(mSelectedCards);
            mCardNumbers.removeAll(mSelectedCards);
            mAdapter.notifyDataSetChanged();
        }
    }

    private class CardHolder extends RecyclerView.ViewHolder {

        TextView cardNumber;
        ImageView checkBox;

        CardHolder(View itemView) {
            super(itemView);
            cardNumber = (TextView) itemView.findViewById(R.id.tv_card);
            checkBox = (ImageView) itemView.findViewById(R.id.iv_checkbox);
        }
    }

    private class CardNumberAdapter extends RecyclerView.Adapter<CardHolder> {

        @Override
        public CardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new CardHolder(View.inflate(parent.getContext(), R.layout.layout_item_card, null));
        }

        @Override
        public void onBindViewHolder(final CardHolder holder, int position) {
            final String cardNumber = mCardNumbers.get(position);
            holder.cardNumber.setText(cardNumber);
            holder.checkBox.setImageResource(mSelectedCards.contains(cardNumber) ? R.mipmap.checkbox_check : R.mipmap.checkbox_uncheck);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
	                logger.debug("Click action cardNumber =>" + cardNumber);
	                if (mSelectedCards.contains(cardNumber)) {
                        holder.checkBox.setImageResource(R.mipmap.checkbox_uncheck);
                    } else {
                        holder.checkBox.setImageResource(R.mipmap.checkbox_check);
                        mSelectedCards.add(cardNumber);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mCardNumbers.size();
        }
    }
}
